package com.csnj.core.Srvs.SysManager;

import com.csnj.common.biz.BaseBiz;
import com.csnj.core.constant.CoreConstants;
import com.csnj.core.dao.SysMenuMapper;
import com.csnj.core.entity.SysMenu;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xiabin on 2018/11/22.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysMenuSrvs extends BaseBiz<SysMenuMapper, SysMenu> {

    public List<SysMenu> getUserAuthorityMenuByUserId(Integer uid) {
        return mapper.selectAuthorityMenuByUserId(uid);
    }

    @Override
    public void insertSelective(SysMenu entity) {
        if (CoreConstants.ROOT == entity.getParentId()) {
            entity.setPath("/" + entity.getCode());
        } else {
            SysMenu parent = this.selectById(entity.getParentId());
            entity.setPath(parent.getPath() + "/" + entity.getCode());
        }
        super.insertSelective(entity);
    }

    @Override
    public void updateById(SysMenu entity) {
        if (CoreConstants.ROOT == entity.getParentId()) {
            entity.setPath("/" + entity.getCode());
        } else {
            SysMenu parent = this.selectById(entity.getParentId());
            entity.setPath(parent.getPath() + "/" + entity.getCode());
        }
        super.updateById(entity);
    }

    @Override
    public void updateSelectiveById(SysMenu entity) {
        super.updateSelectiveById(entity);
    }

    /**
     * 获取用户可以访问的菜单
     *
     * @param id
     * @return
     */
    public List<SysMenu> getUserAuthorityMenuByUserId(int id) {
        return mapper.selectAuthorityMenuByUserId(id);
    }

    /**
     * 根据用户获取可以访问的系统
     *
     * @param id
     * @return
     */
    public List<SysMenu> getUserAuthoritySystemByUserId(int id) {
        return mapper.selectAuthoritySystemByUserId(id);
    }
}
